<?php

/**
 * Description of ContentEmployeeNews
 *
 * @author Honza, Petr
 */
class ContentEmployeeNews implements IMainBodyWithInteraction {

  private $showTable = true;
  private $showForm = true;
  private $error;

  public function action() {
    //delete news from table
    if ($_GET['a'] == 'erase') {
      $sql = 'delete from ' . TBL_NEWS . ' where id_news = ' . $_GET['ida'];
      DB::sql($sql);
    }
  }

  public function applyForm($formID = NULL) {
    if (isset($_POST['newSubmit'])) {
      $title = addslashes($_POST['title']);
      $text = $_POST['text'];

      if (!$title || !$text) { //check 
        $this->error = '<p class="error">All fields have to be filled</p>';
      } else {
        $this->showForm = false; // insert to table
        $sql = 'insert into ' . TBL_NEWS . " values(0,'$title','$text',CURDATE())";
        DB::sql($sql);
      }
    }

//editSubmit
    if (isset($_POST['editSubmit'])) {
      $title = addslashes($_POST['title']);
      $text = $_POST['text'];
      $newsID = $_POST['ida'];

      if (!$title || !$text) { //check
        $this->error = '<p class="error">All fields have to be filled</p>';
      } else {
        $this->showForm = false; //udpate in table
        $sql = 'update ' . TBL_NEWS . " set title='$title', text='$text' where id_news=$newsID";
        DB::sql($sql);
      }
    }
  }

  public function getContent() {
    $content = '<h2>News</h2>';
    
    $this->action();
    $this->applyForm();
    
    $form = $this->getForm();
    if ($this->showTable) {
      $content .= '<a href="index.php?where=employee&subwhere=news&a=new">Add news</a>';
    }
    $content .= $form;
    $content .= $this->getTable();

    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
    //draw form for inserting
    if ($_GET['a'] == 'new' && $this->showForm) {
      $this->showTable = false;

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Title', 'title')->addInput('text', 'title', $_POST['title']);

      $f->addTextArea('text', $_POST['text']);

      $f->addLabel('&nbsp', 'newSubmit')->addInput('submit', 'newSubmit', 'Create news', array("class" => "submit"));
      $f->endForm();

      return Content::getWisiwigScript() . $this->error . $f->getForm(); //create what you see editor
    }

    //draw form for editing news by ID
    if ($_GET['a'] == 'edit' && $this->showForm) {
      $newsID = addslashes($_GET['ida']);
      $sql = 'select title, text from ' . TBL_NEWS . ' where id_news = ' . $newsID;
      $news = DB::sql($sql);

      $title = $news[0]['title'];
      $text = $news[0]['text'];

      $this->showTable = false;

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Title', 'title')->addInput('text', 'title', $title);
      $f->addInput('hidden', 'ida', $newsID);

      $f->addTextArea('text', $text);

      $f->addLabel('&nbsp', 'editSubmit')->addInput('submit', 'editSubmit', 'Edit news', array("class" => "submit"));
      $f->endForm();

      return Content::getWisiwigScript() . $this->error . $f->getForm();
    }
  }

  public function getSubmitedForm() {
    
  }

  public function getTable($tableID = NULL) {

    //draw table with news
    if ($this->showTable) {


      $sql = 'select id_news, title, DATE_FORMAT(date, \'%Y-%m-%d\') as datum from ' . TBL_NEWS;

      $result = DB::sql($sql);

      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Title</th><th>Date</th><th>Edit</th><th>Erase</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $idNov = $row['id_news'];

        $table .= '<tr>';
        $table .= '<td>' . $row['title'] . '</td>';
        $table

                .= '<td>' . $row['datum'] . '</td>';
        $table .= '<td><a href="index.php?where=employee&subwhere=news&a=edit&ida=' . $idNov . '">Edit</a></td>';
        $table .= '<td><a href="index.php?where=employee&subwhere=news&a=erase&ida=' . $idNov . '">Erase</a></td>';
        $table .= '</tr>';
      }
      $table .= '';
      $table .= '</table>';

      return $table

      ;
    }
  }

}

?>
